﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;
using System.Windows.Forms;

namespace DevEx
{
    public class CDPNX
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }
                
        public IQueryable<tblDPNX> SelectAllRecord()
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblDPNXes
                    select p;
            return q;
        }

        public tblDPNX SelectRecordByID(int ID_DPNX)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDPNXes where p.ID_DPNX == ID_DPNX select p).SingleOrDefault();
            return q;
        }

        public void XacNhanPhieu(int ID_DPNX, int ID_User)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDPNXes where p.ID_DPNX == ID_DPNX select p).SingleOrDefault();

            q.ID_ApproveUser = ID_User;
            dc.SubmitChanges();

            // Cập nhật SLTH
            CSLNX m_SLNX = new CSLNX ();
            CTon m_ton = new CTon();
            var list_SLNX = m_SLNX.SelectAllRecord(ID_DPNX);
            foreach (tblSLNX obj_SLNX in list_SLNX)
            { 
                //1. Insert SLTH trong Tồn                
                int ID_MatHang = Convert.ToInt32(obj_SLNX.ID_MatHang);
                double SLTH = Convert.ToDouble(obj_SLNX.SLKH);
                m_ton.InsertSLTH(q.NX, ID_MatHang, SLTH);

                //2. Update SLTH = SLKH trong tblSLNX
                obj_SLNX.SLTH = obj_SLNX.SLKH;
                m_SLNX.UpdateSLTH(obj_SLNX); 
            }
        }

        public void HuyXacNhanPhieu(int ID_DPNX, int ID_User)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDPNXes where p.ID_DPNX == ID_DPNX select p).SingleOrDefault();

            if (q.ID_ApproveUser == ID_User)
            {
                q.ID_ApproveUser = null;
                dc.SubmitChanges();
            }
            else { MessageBox.Show("Bạn không phải là người xác nhận phiếu"); }


            // Cập nhật SLTH
            CSLNX m_SLNX = new CSLNX();
            CTon m_ton = new CTon();
            var list_SLNX = m_SLNX.SelectAllRecord(ID_DPNX);
            foreach (tblSLNX obj_SLNX in list_SLNX)
            {
                //1. Insert SLTH trong Tồn
                int ID_MatHang = Convert.ToInt32(obj_SLNX.ID_MatHang);
                double SLTH = Convert.ToDouble(obj_SLNX.SLKH);
                m_ton.DeleteSLTH(q.NX,ID_MatHang, SLTH);

                //2. Update SLTH = SLKH trong tblSLNX                
                obj_SLNX.SLTH = 0;
                m_SLNX.UpdateSLTH(obj_SLNX);
            }            
        }

        //Insert
        public int Insert(tblDPNX obj)
        {
            int ID_DPNX;
            QLVTDataContext dc = GetDc();
            dc.tblDPNXes.InsertOnSubmit(obj);            
            dc.SubmitChanges();
            ID_DPNX = obj.ID_DPNX;

            return ID_DPNX;
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();
            var DPNX = dc.tblDPNXes.SingleOrDefault(p => p.ID_DPNX == ID);
            String NX = DPNX.NX;
            //1. Xóa SLNX và tồn
            CSLNX m_SLNX = new CSLNX();
            CTon m_Ton = new CTon();

            var list_SLNX = m_SLNX .SelectAllRecord (ID);

            foreach (tblSLNX obj_SLNX in list_SLNX)
            {
                int ID_MatHang = Convert.ToInt32(obj_SLNX.ID_MatHang);
                Double SLKH = Convert.ToDouble(obj_SLNX .SLKH);
                int ID_SLNX = Convert.ToInt32(obj_SLNX.ID_SLNX);
                
                //a. Xóa tồn
                if (NX == "N")                
                    m_Ton.DeleteNhap(ID_MatHang ,SLKH);         
                
                if (NX == "X")
                    m_Ton.DeleteXuat(ID_MatHang, SLKH);
                
                //b. Xóa SLNX
                m_SLNX.Delete(ID_SLNX);
            }
            //2. Xóa DPNX
            
            dc.tblDPNXes.DeleteOnSubmit(DPNX);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblDPNX obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblDPNXes.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
        
    }
}
